package com.ddwl.common.aop;


import brave.Span;
import brave.Tracer;
import brave.http.HttpTracing;
import brave.propagation.CurrentTraceContext;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Aspect
@Component
public class XXJobTraceAop {

    @Resource
    private HttpTracing httpTracing;
    @Resource
    private Tracer tracer;


    @Around("@annotation(com.xxl.job.core.handler.annotation.XxlJob)")
    public Object interceptor(ProceedingJoinPoint pjp) throws Throwable {
        Span span = tracer.newTrace();
        CurrentTraceContext.Scope scope = httpTracing.tracing().currentTraceContext().newScope(span.context());
        try {
            return pjp.proceed();
        } finally {
            scope.close();
        }
    }
}
